package cn.bonoon.kernel.web.models;

import javax.servlet.http.HttpServletRequest;

import cn.bonoon.kernel.util.MD5Util;
import cn.bonoon.kernel.util.StringHelper;

public class FormModel extends StandardModel {
	
	private final static String DEFAULT_FORM_NAME = "fo";
	private final static String DEFAULT_FORM_MASTER = "layout-form.vm";
	
	private final String id;

	private final StringBuilder html = new StringBuilder();
	
	public FormModel(String mid, HttpServletRequest request) {
		super(mid, request);
		if(StringHelper.isEmpty(mid)){
			id = MD5Util.randomMD5String();
		}else{
			id = mid;
		}
		setMaster(DEFAULT_FORM_MASTER);
		setModelName(DEFAULT_FORM_NAME);
	}
	
	private int buttonCount = 0;
	
	public void button(String name, String href){
		String id = "btn-" + this.id + "-" + buttonCount++;
		html.append("<a href='").append(href)
			.append("' id='").append(id)
			.append("' title='").append(name)
			.append("'>").append(name).append("</a>");
		block.append("jQuery('#").append(id)
			.append("').click(btn_").append(this.id).append("_click);");
	}
	
	public StringBuilder getHtml() {
		return html;
	}

	public String getId() {
		return id;
	}
}
